package com.chwn.curiousdiary.gloable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.api.IUserApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.ui.login.LoginActivity;
import com.chwn.curiousdiary.utils.DialogUtil;
import com.chwn.curiousdiary.utils.SPUtils;
import com.chwn.harulib.mvp.rx.HttpResult;

import java.util.List;

import cn.jpush.android.api.JPushInterface;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;


/**
 * Description
 * Author by myshikin
 * Created  on 2017/08/07.
 * Version 1.0
 * Desc 用户操作帮助类(收藏 、分享 、购买、充值 、登录判断)
 */
public class UserUtils {
    public static final String KEY_USER = "mUser";


    private static final int COLLECT    = 1;
    private static final int NO_COLLECT = 0;

    public static void saveUser(Context context, User user) throws Exception {
        if (user == null) {
            SPUtils.delete(context, KEY_USER);
        } else {
            SPUtils.saveBean(KEY_USER, user);
        }
    }

    public static User getUser() throws Exception {
        return (User) SPUtils.getBean(KEY_USER, User.class);
    }


    public interface UserActionCallBack {
        void onSuccess();

        void onFail();
    }

    public interface UserLoginCallBack {
        void onStart();

        void onSuccess(User user);

        void onFail(String message);
    }


    public static void userLogin(int loginWay, String openId, String unionId, String nickName, String headUrl, Context context , @NonNull UserLoginCallBack callBack) {
        userLogin(null, null, loginWay, JPushInterface.getRegistrationID(context), openId, unionId, nickName, headUrl , callBack);
    }


    public static void userLogin(String phone, String password, int loginWay, Context context ,  @NonNull UserLoginCallBack callBack) {
        userLogin(phone, password, loginWay, JPushInterface.getRegistrationID(context), null, null, null, null , callBack);
    }


    private static void userLogin(String phone, String passWord, int loginWay, String jPushId, String openId, String unionId, String nickName, String headUrl , final UserLoginCallBack callBack) {
        NetWork.getInstance()
                .getApiService(IUserApi.class)
                .userLogin(phone, passWord, loginWay, jPushId, openId, unionId, Constant.OSTYPE, nickName, headUrl)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                             callBack.onStart();
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HttpResult<List<User>>>() {
                    @Override
                    public void onCompleted() {
                        DialogUtil.dismissWaitDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.onFail("");
                    }

                    @Override
                    public void onNext(HttpResult<List<User>> userHttpResult) {
                        if (userHttpResult.getResult() == Constant.RESULT_OK) {
                             callBack.onSuccess(userHttpResult.getResdata().get(0));
                        } else{
                             callBack.onFail(userHttpResult.getErrorMessage());
                        }
                    }
                });
    }

    /**
     *
     **/
    public void share() {

    }

    /**
     * 用户登录判断
     * @param activity
     * @return
     */
    public static boolean isLogin(final Activity activity) {
        if (!DiaryApplication.mInstance.isLogin()) {
            Intent intent = new Intent(activity, LoginActivity.class);
            intent.putExtra(Constant.IS_LOGIN, Constant.REQUEST_LOGIN_CODE);
            activity.startActivityForResult(intent, Constant.REQUEST_LOGIN_CODE);
            return false;
        }
        return true;
    }

    /**
     * 用户登录判断
     * @param context
     * @return
     */
    public static boolean isLogin(final Context context) {
        if (!DiaryApplication.mInstance.isLogin()) {
            Intent intent = new Intent(context, LoginActivity.class);
            intent.putExtra(Constant.IS_LOGIN, Constant.REQUEST_LOGIN_CODE);
            context.startActivity(intent);
            return false;
        }
        return true;
    }

    /**
     * 用户登录判断
     * @param fragment
     * @return
     */
    public static boolean isLogin(final Fragment fragment) {
        if (!DiaryApplication.mInstance.isLogin()) {

            Intent intent = new Intent(fragment.getActivity(), LoginActivity.class);
            intent.putExtra(Constant.IS_LOGIN, Constant.REQUEST_LOGIN_CODE);
            fragment.startActivityForResult(intent, Constant.REQUEST_LOGIN_CODE);
            return false;
        }
        return true;
    }

}
